﻿using System;
using Autofac.Builder;
using WeDonekRpc.Client.Interface;

namespace WeDonekRpc.Client
{
    public class RpcStartBuilder : IDisposable
    {
        public RpcInitOption Option { get; }

        private event Action<RpcInitOption> _LoadEvent;
        private event Action<IIocService> _InitEvent;
        public ContainerBuildOptions AutofacOptions { get; set; } = ContainerBuildOptions.None;

        public RpcStartBuilder ( IRpcEvent evs )
        {
            this.Option = new RpcInitOption(evs); ;
        }
        public void Config ( Action<RpcInitOption> option )
        {
            option(this.Option);
        }
        public void AddLoadEvent ( Action<RpcInitOption> opt )
        {
            _LoadEvent += opt;
        }
        public void AddInitEvent ( Action<IIocService> opt )
        {
            _InitEvent += opt;
        }
        public void Load ()
        {
            this.Option.Load(_LoadEvent);
        }
        public void Dispose ()
        {
            this.Option.Dispose();
        }
        public void Build ()
        {
            this.Option.Init(_InitEvent, this.AutofacOptions);
            RpcClient.Start();
            this.Dispose();
        }
    }
}
